#!/usr/bin/env python
#-*- coding:utf-8 -*-

#scp -i /users/zhangchong/ssh/10.103.13.15 -p -C  -P 22022 root@10.103.13.15:/root/wap_stat.sh ./

'''
Created on 2012-01-12

@author: zhangchong
'''


#/data/log/log/[0-9,a-z]*/[0-9]*[0-9]
#chmod 700 /users/zhangchong/ssh/
#chmod 600 /users/zhangchong/ssh/*

import sys
import os
import string
import Config
import time
import commands

Server = Config.Server
Port = Config.Port    
statisPathList = [
            ("statis_log", "25"),
            ("statis_log", "32"),
            ("statis_log", "33"),
            ("statis_log", "34"),
            ("statis_log", "36"),
            ("statis_log", "37"),
    ]

accessPathList = [
            ("access_log", "25"),
            ("access_log", "32"),
            ("access_log", "33"),
            ("access_log", "34"),
            ("access_log", "36"),
            ("access_log", "37"),
    ]


wapPathList = [
            ("log", "13"),
            ("log", "26"),
    ]




SaveLogDir = Config.SaveLogDir
sortSh = Config.ScriptsDir + "sort.sh"

def cpLog(date, type="all", isReWite=False, localDir="", saveCatSortTar=False):

    date = str(date)
    type = str(type)
    
    fileList = ""
    
    if(type == "all"):
        cpLog(date=date, type="wap", localDir=localDir)
        cpLog(date=date, type="statis", localDir=localDir)
        cpLog(date=date, type="access", localDir=localDir)
        return
     
    if(not os.path.exists(Config.TempDir)):
        os.makedirs(Config.TempDir)
        
     
    if(type == "statis"):
        pathList = statisPathList
    
    if(type == "access"):
        pathList = accessPathList 
        
    if(type == "wap"):
        pathList = wapPathList
        for file, server in pathList:
            tmpfile = file + "." + str(date) + ".tar.gz"
            cmd = "scp -P " + str(Port) + " -i " + Config.SSHKeyPath + " root@" + Server + ":" + Config.BackupServerLogDir + server + "/" + tmpfile + " " + Config.TempDir + tmpfile
            
            if(localDir != ""):
                cmd = "cp " + localDir + server + "/" + tmpfile + " " + Config.TempDir + tmpfile
        
            print cmd
            (status, result) = commands.getstatusoutput(cmd)
            print status, result
            if(status == 0):
                cmd = "tar zxvf " + Config.TempDir + tmpfile + " -C " + Config.TempDir
                print cmd
                result = os.popen(cmd)
                result = result.read()
                print result
                
                cmd = "mv " + Config.TempDir + "log." + date + " " + Config.TempDir + file + "_" + date + "_" + server
                print cmd
                result = os.popen(cmd)
                result = result.read()
                print result
                
                cmd = "rm -fr  " + Config.TempDir + tmpfile
                print cmd
                result = os.popen(cmd)
                result = result.read()
                print result
                
                fileList = fileList + " " + Config.TempDir + file + "_" + date + "_" + server
            
        if(not os.path.exists(SaveLogDir + "wap/")):
            os.makedirs(SaveLogDir + "wap/")
        if(len(fileList.lstrip().rstrip()) == 0):
            return
        cmd = "cat " + fileList + " > " + SaveLogDir + "wap/" + date
        print cmd
        result = os.popen(cmd)
        result = result.read()
        print result
        
        cmd = "rm -fr " + fileList
        print cmd
        result = os.popen(cmd)
        result = result.read()
        
        print "\n"
            
        return     
        
        
    if(isReWite == False):   
         for file, server in pathList: 
             sortTarLog = SaveLogDir + file + "_" + date + "_sort" + ".tar.gz"
             if(os.path.exists(sortTarLog)):
                 filesize = os.path.getsize(sortTarLog)
                 print sortTarLog + " size:" + str(filesize) + " byte"
                 if(filesize < 1000):
                     cmd = "rm -fr " + sortTarLog
                     print cmd
                     (status, result) = commands.getstatusoutput(cmd)
                     print status, result
                 else:
                     sortLog = SaveLogDir + file + "_" + date + "_sort"  
                     cmd = "rm -fr " + sortLog
                     print cmd
                     (status, result) = commands.getstatusoutput(cmd)
                     print status, result
                        
                     cmd = "tar zxvf " + sortTarLog + " -C " + Config.SaveLogDir
                     print cmd
                     (status, result) = commands.getstatusoutput(cmd)
                     print status, result
                     if(status == 0):
                        return
                     else:
                        cmd = "rm -fr " + sortTarLog
                        print cmd
                        (status, result) = commands.getstatusoutput(cmd)
                        print status, result
                        
                        sortLog = SaveLogDir + file + "_" + date + "_sort"  
                        cmd = "rm -fr " + sortLog
                        print cmd
                        (status, result) = commands.getstatusoutput(cmd)
                        print status, result
             
             break
        

    
    for file, server in pathList:
        tmpfile = file + "." + str(date) + ".tar.gz"
        cmd = "scp -P " + str(Port) + " -i " + Config.SSHKeyPath + " root@" + Server + ":" + Config.BackupServerLogDir + server + "/" + tmpfile + " " + Config.TempDir + tmpfile
        
        if(localDir != ""):
            cmd = "cp " + localDir + server + "/" + tmpfile + " " + Config.TempDir + tmpfile
    
        print cmd
        (status, result) = commands.getstatusoutput(cmd)
        print status, result
        if(status == 0):
            cmd = "tar zxvf " + Config.TempDir + tmpfile + " -C " + Config.TempDir
            print cmd
            result = os.popen(cmd)
            result = result.read()
            print result
            
            cmd = "mv " + Config.TempDir + "log." + date + " " + Config.TempDir + file + "_" + date + "_" + server
            print cmd
            result = os.popen(cmd)
            result = result.read()
            print result
            
            cmd = "rm -fr  " + Config.TempDir + tmpfile
            print cmd
            result = os.popen(cmd)
            result = result.read()
            print result
            
            fileList = fileList + " " + Config.TempDir + file + "_" + date + "_" + server
            
    
    cmd = "rm -fr  " + file + "_" + date + "_sort"
    print cmd
    result = os.popen(cmd)
    result = result.read()
    print result
    
    cmd = "rm -fr  " + file + "_" + date + "_sort" + ".tar.gz"
    print cmd
    result = os.popen(cmd)
    result = result.read()
    print result
    
    if(len(fileList.lstrip().rstrip()) == 0):
        return
    starttime = time.time()
    cmd = sortSh + " " + fileList + " > " + file + "_" + date + "_sort"
    print cmd
    result = os.popen(cmd)
    result = result.read()
    endtime = time.time()
    print "Run times:" + str(int((endtime - starttime))) + "\n" 
    
    
    if(saveCatSortTar):
        starttime = time.time()
        cmd = "tar zcvf " + file + "_" + date + "_sort" + ".tar.gz " + file + "_" + date + "_sort"
        print cmd
        result = os.popen(cmd)
        result = result.read()
        endtime = time.time()
        print "Run times:" + str(int((endtime - starttime))) + "\n"  
    
    
    cmd = "rm -fr " + fileList
    print cmd
    result = os.popen(cmd)
    result = result.read()
    
    
    
    if(os.path.realpath(SaveLogDir + file + "_" + date + "_sort" + ".tar.gz") != os.path.realpath(file + "_" + date + "_sort" + ".tar.gz")):
        cmd = "rm -fr " + SaveLogDir + file + "_" + date + "_sort" + ".tar.gz"
        print cmd
        result = os.popen(cmd)
        result = result.read()
        
        cmd = "rm -fr " + SaveLogDir + file + "_" + date + "_sort"
        print cmd
        result = os.popen(cmd)
        result = result.read()
    
        if(saveCatSortTar):
            cmd = "mv " + file + "_" + date + "_sort" + ".tar.gz " + SaveLogDir + file + "_" + date + "_sort" + ".tar.gz"
            print cmd
            result = os.popen(cmd)
            result = result.read()
    
    
        cmd = "mv " + file + "_" + date + "_sort " + SaveLogDir + file + "_" + date + "_sort"
        print cmd
        result = os.popen(cmd)
        result = result.read()
        print result

if __name__ == "__main__":

    argv = sys.argv
    cpdate = argv[1]
    cptype = "all"
    
    isReWrite = False
    try:
        argv2 = argv[2]
        if(argv2.lower() == "true"):
            isReWrite = True
    except:
        isReWrite = False 
    
    try:
        cptype = argv[3]
    except:
        cptype = "all"  
    cpLog(date=cpdate, type=cptype, isReWite=isReWrite)
    
